<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
var echarts = require("echarts");
require("echarts/theme/macarons"); // echarts theme
import resize from "./mixins/resize";
import { showAllBooks } from "@/api/user";
const animationDuration = 3000;

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: "chart",
    },
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "300px",
    },
  },
  data() {
    return {
      chart: null,
      showData: [],
      allcount: "",
    };
  },
  async mounted() {
    await this.getshowAllbooks();
    this.$nextTick(() => {
      this.initChart();
    });
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  created() {},
  methods: {
    async getshowAllbooks() {
      console.log("获取商品总览");
      await showAllBooks().then((res) => {
        console.log(res);
        this.showData = [
          { value: res.data.data.upCount, name: "已上架" },
          { value: res.data.data.disableCount, name: "已下架" },
        ];
        this.allcount = res.data.data.allCount;

        console.log(this.showData);
      });
    },

    initChart() {
      this.chart = echarts.init(this.$el, "macarons");

      this.setOptions(this.showData, this.allcount);
    },
    setOptions(options, count) {
      this.chart.setOption({
        tooltip: {
          trigger: "item",
          textStyle: {
            color: "#fff",
          },
        },
        legend: {
          top: "",
          left: "center",
          data: ["已上架", "已下架"],
        },
        series: [
          {
            name: "Access From",
            type: "pie",

            radius: ["40%", "70%"],
            // avoidLabelOverlap: false,
            // hoverAnimation: false,
            visualMap: {
              piecewise: {
                showLabel: true,
              },
            },
            itemStyle: {
              borderRadius: 10,
              borderColor: "#fff",
              borderWidth: 2,
            },

            label: {
              show: true,

              position: "center",
              formatter: function () {
                return "图书总数量\r\n" + count;
              },
              textStyle: {
                fontSize: 20,
              },
            },

            labelLine: {
              show: false,
            },
            data: options,
          },
          {
            name: "Access From",
            type: "pie",

            radius: ["40%", "70%"],
            // avoidLabelOverlap: false,
            // hoverAnimation: false,
            visualMap: {
              piecewise: {
                showLabel: true,
              },
            },
            itemStyle: {
              borderRadius: 10,
              borderColor: "#fff",
              borderWidth: 2,
            },
          
            label: {
                textStyle: {
                  fontSize: 15,
                },
                 formatter: function (options) {
                  return  options.value+'本';
                },
            },
            labelLine: {
              show: true,
            },
            data: options,
          },
        ],
      });
    },
  },
};
</script>
